package com.solver.controller.admin;

import com.solver.constant.JwtClaimsConstant;
import com.solver.dto.TeacherDTO;
import com.solver.dto.TeacherLoginDTO;
import com.solver.dto.TeacherPageQueryDTO;
import com.solver.entity.Teacher;
import com.solver.properties.JwtProperties;
import com.solver.result.PageResult;
import com.solver.result.Result;
import com.solver.service.TeacherService;
import com.solver.utils.JwtUtil;
import com.solver.vo.TeacherLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param teacherLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<TeacherLoginVO> login(@RequestBody TeacherLoginDTO teacherLoginDTO) {
        log.info("员工登录：{}", teacherLoginDTO);

        Teacher teacher = teacherService.login(teacherLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, teacher.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        //封装的数据交给前端处理 用.builder()方法要求VO类上加上@builder注解
        TeacherLoginVO teacherLoginVO = TeacherLoginVO.builder()
                .id(teacher.getId())
                .userName(teacher.getUsername())
                .name(teacher.getName())
                .token(token)
                .build();

        return Result.success(teacherLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增老师功能
     * @param teacherDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody TeacherDTO teacherDTO){
        log.info("新增教师：{}", teacherDTO);
        teacherService.save(teacherDTO);
        return Result.success(teacherDTO);
    }

    @GetMapping("/page")
    public Result<PageResult> page(TeacherPageQueryDTO teacherPageQueryDTO){
        log.info("分页查询{}", teacherPageQueryDTO);
        PageResult pageResult = teacherService.pagequery(teacherPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("起禁用员工账号：{}，{}",status,id);
        teacherService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Teacher> getById(@PathVariable Long id){
        log.info("修改员工信息，id：{}",id);
        Teacher teacher = teacherService.getById(id);
        teacher.setPassword("*****");
        return Result.success(teacher);
    }

    /**
     * 更新员工信息
     * @param teacherDTO
     * @return
     */
    @PutMapping
    public Result update(@RequestBody  TeacherDTO teacherDTO){
        log.info("更新员工信息");
        teacherService.update(teacherDTO);
        return Result.success();
    }

}
